#
# @lc app=leetcode.cn id=160 lang=python3
#
# [160] 相交链表
#
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
# @lc code=start
# Definition for singly-linked list.


class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        s = set()
        while headA is not None:
            s.add(headA)
            headA = headA.next
        
        while headB is not None:
            if headB in s:
                return headB
            headB = headB.next
        return None
# @lc code=end

